package com.woniuxy.personal.core.mq;

import com.rabbitmq.client.Channel;
import com.woniuxy.common.CommonResult;
import com.woniuxy.personal.client.HomepageFeignClient;
import com.woniuxy.personal.client.dto.CarGoods;
import com.woniuxy.personal.core.constant.OrderConstant;
import com.woniuxy.personal.core.mapper.PlOrderMapper;
import com.woniuxy.personal.core.mapper.PlOrderitemMapper;
import com.woniuxy.personal.core.model.PlOrder;
import com.woniuxy.personal.core.model.PlOrderitem;
import com.woniuxy.personal.core.vo.OrderVo;
import com.woniuxy.personal.core.vo.OrderitemVo;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Auther: binhu  <br/>
 * Date: 2021/6/30:14:36  <br/>
 * Description:
 */
@Component
public class OrderConsumer {

    @Autowired
    private PlOrderMapper orderMapper;

    @Autowired
    private PlOrderitemMapper itemMapper;

    @Autowired
    private HomepageFeignClient homepageFeignClient;

    @RabbitHandler
    @RabbitListener(queues = "order-queue")
    public void  processOrder(@Payload OrderVo orderVo, Channel channel, @Headers Map headers) throws IOException {

        String ordernumber = (String) headers.get("spring_returned_message_correlation");

        //处理订单数据
        PlOrder order = new PlOrder();
        order.setOrdertype(orderVo.getOrdertype());
        order.setStatus(OrderConstant.PENDING_PAYMENT);
        order.setOrdernumber(ordernumber);
        order.setUserId(orderVo.getUserId());
        order.setCreatetime(new Date());
        orderMapper.insert(order);
        PlOrder plOrder = orderMapper.selectByOrdernumber(ordernumber);

        BigDecimal totalPrice = new BigDecimal("0.0");
        List<OrderitemVo> orderitemVos = orderVo.getOrderitemVos();
        for (OrderitemVo orderitemVo:orderitemVos) {
            Integer goodsId = orderitemVo.getGoodsId();
            Integer quantity = orderitemVo.getQuantity();
            Integer firstId = orderitemVo.getFirstId();

            PlOrderitem item = new PlOrderitem();
            CommonResult<CarGoods> carGoodsCommonResult = homepageFeignClient.selectByID(goodsId);
            CarGoods data = carGoodsCommonResult.getData();
            item.setName(data.getGoodsName());
            item.setImg(data.getImg());
            item.setSellPrice(data.getSellPrice());
            item.setPrice(data.getPrice());
            item.setSize(data.getSepcification());
            item.setStoreId(data.getStoreId());
            item.setOrdertype(data.getGoodsStyle());
            item.setQuantity(quantity);
            item.setOrderId(plOrder.getId());
            item.setFirstId(firstId);

            BigDecimal sellPrice = data.getSellPrice();
            BigDecimal count = new BigDecimal(Integer.parseInt(quantity.toString()));
            totalPrice = sellPrice.multiply(count).add(totalPrice);

            itemMapper.insert(item);
        }
            orderMapper.addTotalPrice(totalPrice,ordernumber);

        //签收
        Long id = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(id,false);
        } catch (IOException e) {
            e.printStackTrace();
            channel.basicNack(id,false,false);
        }
    }
}
